package com.nanuminc.market.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.nanuminc.market.R;

public class ImageUtil extends BaseAdapter {

	private static HashMap<String,SoftReference<Bitmap>> cache = new HashMap<String,SoftReference<Bitmap>>();
	public Bitmap mBit;
	
	/** 이미지 겔러리 */
	int mGalleryItemBackground;
	String displayHigh;
	TypedArray typedArray;
	private Context mContext;
	private Integer[] mImageIds = { R.drawable.car1, R.drawable.car2,
					R.drawable.car3, R.drawable.car4, };
	
	public ImageUtil() {

	}
	
	public ImageUtil(Context c) {
		mContext = c;
	}

	public ImageUtil(Context c, String displayHigh) {
		mContext = c;
		this.displayHigh = displayHigh;
	}

	public int getCount() {
		return mImageIds.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView iv;
		
		/*TypedArray typedArray = mContext.obtainStyledAttributes(R.styleable.Gallery1);
        mGalleryItemBackground = typedArray.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);
        typedArray.recycle();*/

		
		if(convertView==null)
			iv = new ImageView(mContext);
		else
			iv = (ImageView)convertView;
		
		iv.setImageResource(mImageIds[position]);
		iv.setScaleType(ImageView.ScaleType.FIT_XY);
		if ("HIGH".equals(this.displayHigh))
			iv.setLayoutParams(new Gallery.LayoutParams(570, 500));
		else
			iv.setLayoutParams(new Gallery.LayoutParams(300, 250));
		
		iv.setBackgroundResource(mGalleryItemBackground);
		return iv;
	}
	
	public Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
	     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
	         bitmap.getHeight(), Config.ARGB_8888); 
	     Canvas canvas = new Canvas(output); 
	     final int color = 0xff424242; 
	     final Paint paint = new Paint(); 
	     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
	     final RectF rectF = new RectF(rect); 
	     final float roundPx = 8; 
	     paint.setAntiAlias(true); 
	     canvas.drawARGB(0, 0, 0, 0); 
	     paint.setColor(color); 
	     canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
	     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
	     canvas.drawBitmap(bitmap, rect, rect, paint); 
	     return output; 
	}

	public static Bitmap getImage(String key) {
		if (cache.containsKey(key)) {
			SoftReference<Bitmap> ref = cache.get(key);
			if (ref != null) {
				return ref.get();
			}
		}
		return null;
	}
	
	public static  void setImage(String key, Bitmap image) {
		cache.put(key, new SoftReference<Bitmap>(image));
	}
		 
	public  boolean hasImage(String key) {
		return cache.containsKey(key);
	} 
		  
	public static void clear() {
		cache.clear();
	}
	

	public  void ApiUrlToBitmap(Context context, String cmd) {
		String path = Environment.getDataDirectory().getAbsolutePath();
		path += "/data/com.nanuminc/test.jpg";		 
		File file = new File(path);

		if(file.exists()) {
			mBit =BitmapFactory.decodeFile(path);
			return;
		}
		 		
		String imgurl = cmd;
		try {
			InputStream is  = new URL(imgurl).openStream();
			mBit = BitmapFactory.decodeStream(is);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public Bitmap GetBitmap(){
		return mBit;
	}
}
